
<template>
    <div>
        <el-tabs v-model="activeName">

            <el-tab-pane label="基础信息" name="Datas">

                <el-form :model="model" :rules="rules" ref="ruleForm" label-width="100px"
                    class="demo-ruleForm page-form">
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="报价单号" prop="Number">
                                <el-input v-model="model.Number"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="客户" prop="CustomerNumber">
                                <el-select :value="model.CustomerName" placeholder="请选择客户" @change="CustomerChange">
                                    <el-option v-for="Customer in ClientList" :key="Customer.Id"
                                        :label="Customer.ClientName" :value="Customer"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="联系人" prop="CustomerContact">
                                <el-input v-model="model.CustomerContact"></el-input>
                            </el-form-item>
                        </el-col>



                    </el-row>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="联系人电话" prop="CustomerContactTel">
                                <el-input v-model="model.CustomerContactTel"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="报价人" prop="EmployeesName">
                                <el-input v-model="model.EmployeesName" placeholder="请选择报价人" :readonly="true"
                                    @focus="selectedPerson"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="交货日期" prop="DeliveryTime">
                                <el-date-picker v-model="model.DeliveryTime" type="datetime"
                                    placeholder="选择日期时间"></el-date-picker>
                            </el-form-item>
                        </el-col>


                    </el-row>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="出库仓" prop="WarehouseId">
                                <el-select :value="model.WarehouseName" placeholder="请选择出库仓" @change="WarehouseChange">
                                    <el-option v-for="Warehouse in WarehouseList" :key="Warehouse.Id"
                                        :label="Warehouse.WarehouseName" :value="Warehouse"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">

                            <el-form-item label="交货地址" prop="DeliveryLocation">
                                <el-input v-model="model.DeliveryLocation"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="收款方式" prop="PaymentCategory">
                                <el-select v-model="model.PaymentCategory">
                                    <el-option label="现金交易" value="现金交易"></el-option>
                                    <el-option label="银行支收" value="银行支收"></el-option>
                                    <el-option label="微信支收" value="微信支收"></el-option>
                                    <el-option label="支收宝支收" value="支收宝支收"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>


                    </el-row>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="收款账户" prop="PaymentAccount">
                                <el-input v-model="model.PaymentAccount"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">

                            <el-form-item label="收款日期" prop="PaymentTime">
                                <el-date-picker v-model="model.PaymentTime" type="datetime"
                                    placeholder="选择日期时间"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="运输方式" prop="Transfer">
                                <el-select v-model="model.Transfer">
                                    <el-option label="公路运输" value="公路运输"></el-option>
                                    <el-option label="铁路运输" value="铁路运输"></el-option>
                                    <el-option label="水路运输" value="水路运输"></el-option>
                                    <el-option label="航空运输" value="航空运输"></el-option>
                                </el-select>

                            </el-form-item>


                        </el-col>


                    </el-row>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="总价" prop="Price">
                                <el-input v-model="model.Price" @change="handleTaxChange" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="优惠" prop="DisCount">
                                <el-input v-model="model.DisCount" readonly></el-input>

                            </el-form-item>
                        </el-col>



                        <el-col :span="8">
                            <el-form-item label="备注" prop="Remark">
                                <el-input v-model="model.Remark" @change="handleTaxChange"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>





                </el-form>


            </el-tab-pane>

            <el-tab-pane label="添加商品" name="AddProduct">


                <div class="page-tool">
                    <el-button type="primary" plain icon="el-icon-plus" @click="addMachine" size="small">新增</el-button>
                    <el-button type="danger" plain icon="el-icon-delete" size="small" @click="delMachine">删除</el-button>
                </div>


                <el-table :data="tableData"  ref="dataForm" v-loading="isLoading" height="300" show-summary  :summary-method="getSummaries">
                    <el-table-column type="selection" width="70"></el-table-column>
                    <el-table-column prop="ItemCode" label="物料编码" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="ItemName" label="物料名称" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="Spec" label="规格" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="UnitOfMeasure" label="单位" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="Amount" label="金额" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="TaxPrice" label="含税单价" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="TaxAmount" label="含税金额" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="CurrCount" label="当前库存" :show-overflow-tooltip="true"></el-table-column>


                    <el-table-column prop="Count" label="数量" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.Count" @change="handleTaxChange(scope.row)" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="Price" label="商品单价" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.Price" @change="handleTaxChange(scope.row)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="Tax" label="税率" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.Tax" @change="handleTaxChange(scope.row)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="Rebate" label="折扣" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.Rebate" @change="handleTaxChange(scope.row)"></el-input>
                        </template>
                    </el-table-column>

                    <el-table-column prop="Remark" label="备注" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.Remark" />
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <el-button type="text" @click="del(scope.row)">移除</el-button>
                        </template>
                    </el-table-column>

                </el-table>
            </el-tab-pane>

        </el-tabs>
        <div class="page-form-action">
            <el-button @click="resetForm">重置</el-button>
            <el-button type="primary" @click="submitForm">{{ isUpdate ? '立即保存' : '立即创建' }}</el-button>
        </div>





        <!-- 弹窗 -->
        <el-dialog append-to-body :close-on-click-modal="false" :title="dialogDomain.title"
            :visible.sync="dialogDomain.dialogVisible" width="60%">
            <SelectedPerson :IsDefault="true" :IsFilter="false" :IsFirst="true" :PersonList="personList"
                @callback="AddPersonCallBack" v-if="dialogDomain.dialogVisible&& dialogDomain.dialogType == 0" />
            <SelectedItemFourStock :WareHouseId="this.model.WarehouseId.toString()"  :ItemList="tableData" @callback="AddBomCallBack"
                v-else-if="dialogDomain.dialogVisible && dialogDomain.dialogType == 1" />

        </el-dialog>
    </div>

</template>
<script>

import SelectedPerson from '@/views/Plans/PlansView/components/SelectedPerson.vue';
import SelectedItemFourStock from '@/views/Product/ProductView/components/SelectedItemFourStock.vue'
export default{
    
    components:{
        SelectedPerson,
        SelectedItemFourStock
    },
    props: {
        model: {
            require: true,
            type: Object
        },
        isUpdate: {
            require: true,
            type: Boolean
        },
        ClientList:{
            type:Array,
            required:true

        },
        WarehouseList:{
            type: Array,
            required: true

        },
        
        EmployeesList:{
            type: Array,
            required: true

        },
        
    },
    data(){
        return{
            
            Amount:'0',
            TaxAmount:'0',
            Tax:'0.00',
            activeName: 'Datas',
            personList:[],
            pageDomain: {
                PageIndex: 1,
                PageSize: 10,
                TotalCount: 0,
            },
            tableData: [],
            isLoading: false,
            //对话框对象
            dialogDomain: {
                title: '报价人选择',
                dialogVisible: false,
                dialogType: 0  //0选择报价人 1选择商品
            },
            rules: {
                Number: { required: true, message: '请输入报价单号', trigger: 'blur' },
                CustomerNumber: { required: true, message: '请选择客户', trigger: 'blur' },
                CustomerContact: { required: true, message: '请输入联系人名称', trigger: 'blur' },
                CustomerContactTel: { required: true, message: '请输入联系人电话', trigger: 'blur' },
                EmployeesName: { required: true, message: '请选择报价人' },
                WarehouseId: { required: true, message: '请选择出库仓'},
            }
            
        }


        
    },
    created() {
        if(this.isUpdate)
            this.GetOrderQuotebyId();
        
    },
    methods: {

        calcPrice(){
             //计算总价
             let sum =0.0;
             let sumRebate =0.0;
             this.tableData.map(item=>{
                sum+=item.Amount;

                if(item.Rebate < 1 ){
                    sumRebate+=item.Price*item.Count*item.Rebate;
                }
            })
            this.model.Price= sum;
            this.model.DisCount= sumRebate;
        },
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = '总价';
                    return;
                }
                if (column.property === 'Amount' ||column.property === 'TaxPrice' ||column.property === 'TaxAmount' || column.property === 'Price') {
                    const values = data.map(item => Number(item[column.property]));
                    

                    if (!values.every(value => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                                
                            } else {
                                return prev;
                            }
                        }, 0);
                        sums[index] += ' 元';
                    } else {
                        sums[index] = '';
                    }
                }
                else {
                    sums[index] = '';
                }
                
            });
            return sums;

            
        },
    


        CustomerChange(model) {
            this.model.CustomerNumber = model.Id;
            this.model.CustomerName = model.ClientName;
            console.log(model)
        },
        WarehouseChange(model) {
            this.model.WarehouseId = model.Id;
            this.model.WarehouseName=model.WarehouseName
            console.log(model)
        },
        //选择报价人
        selectedPerson(model) {
            this.dialogDomain.title = "选择报价人";
            this.dialogDomain.dialogVisible = true;

            this.dialogDomain.dialogType = 0;

        },
        AddPersonCallBack() {
            this.model.EmployeesId =this.personList[0].Id;
            this.model.EmployeesName =this.personList[0].Name;
            this.dialogDomain.dialogVisible = false;
        },
        //添加商品
        addMachine() {
            if(this.model.WarehouseId=='' || this.model.WarehouseId==undefined){
                this.showNotify('请选择仓库');
                return;
            }
            this.dialogDomain.title = "选择商品";
            this.dialogDomain.dialogVisible = true;
            this.dialogDomain.dialogType = 1;
        },

        handleTaxChange(row) {
            if(row.Tax>0){
                row.TaxPrice = row.Price * (1 + Number(row.Tax)); // 含税单价
                row.TaxAmount = row.TaxPrice * row.Count; // 含税金额
            }
            else{
                row.TaxPrice = row.Price; // 含税单价
                row.TaxAmount = row.TaxPrice * row.Count;
            }
            row.Amount = row.Count * row.Price;//金额
            if (row.Rebate < 1) {
                row.TaxPrice = (row.Price * (1 + Number(row.Tax))) * row.Rebate; // 打折后的含税单价
                row.TaxAmount = (row.TaxPrice * row.Count); // 打折后的含税金额
                row.Amount = row.Count * row.Price * row.Rebate;//金额
            }
            
            this.calcPrice();
            
        },
        delMachine() {
            if (this.$refs.dataForm.selection.length <= 0) {
                this.showNotify('请选择一项');
                return;
            }

            this.$refs.dataForm.selection.forEach(row => {
                const index = this.tableData.indexOf(row);
                this.tableData.splice(index, 1);
            });
        },
        //移除商品
        del(model) {
            let index = this.tableData.indexOf(model);
            this.tableData.splice(index, 1);
        },

        submitForm() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {

                    if (this.isUpdate) {
                        this.put();
                    }
                    else {
                        this.add();
                    }

                } else {
                    this.showNotify('请输入必填项');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs['ruleForm'].resetFields();
        },
        add() {
            this.$http.postForm('/api/OrderQuote', { model: this.model,OrderQuoteItemString:JSON.stringify(this.tableData) }, { loading: this.$loading() })
            .then(res => {
                this.$emit('callback');
                
            console.log(res); 
            })

        },
        //修改
        put() {
            this.$http.putForm('/api/OrderQuote', { model: this.model,OrderQuoteItemString:JSON.stringify(this.tableData) }, { loading: this.$loading() })
                .then((res) => {
                    this.$emit('callback');
                })
                console.log(res); 
        },

        GetOrderQuotebyId() {
            this.isLoading=true;
            const response =this.$http.get('/api/OrderQuote/GetOrderQuotebyId',{
                params:{
                    OrderQuoteId:this.model.Id,
                    WareHouseId:this.model.WarehouseId
                }
                
            })
            
            .then(res => {
                    console.log(res)
                    this.isLoading = false;
                    
                    
                    this.tableData=res.Data;
                    
                })
        },
   

        //新增或编辑页面回调
        AddBomCallBack() {
            this.dialogDomain.dialogVisible = false;
            console.log(this.tableData)


            let arr = [];
            this.tableData.map(x => { 
                console.log(x);
                    var obj = new Object();
                    //处理所需字段
                    this.$set(obj, 'Amount', x.Amount|| x.SalePrice  );
                    this.$set(obj, 'TaxAmount', x.TaxAmount|| 0);
                    this.$set(obj, 'TaxPrice', x.TaxPrice|| 0);
                    this.$set(obj, 'Tax', x.Tax || 0);
                    this.$set(obj, 'Remark', x.Remark);
                    this.$set(obj, 'Price', x.Price ||x.SalePrice);
                    this.$set(obj, 'Count', x.Count || 1);
                    this.$set(obj, 'ItemId', x.Id);
                    this.$set(obj, 'Rebate', x.Rebate || 1);
                    this.$set(obj, 'Spec', x.Spec);
                    this.$set(obj, 'ItemName', x.ItemName);
                    this.$set(obj, 'CurrCount', x.CurrCount);
                    this.$set(obj, 'ItemCode', x.ItemCode);
                    this.$set(obj, 'UnitOfMeasure', x.UnitOfMeasure);
                    arr.push(obj);
            })

            this.tableData = arr;

            this.calcPrice();

        },

        
    }
}
</script>